home *** CD-ROM | disk | FTP | other *** search
Text File | 1992-04-21 | 24.9 KB | 692 lines | [TEXT/KAHL] |
- THE SCRIPT LANGUAGE
-
-
- The script language interpreted by "Terminal" is a subset of C with many
- specialized intrinsic (built-in) functions. Please read a C reference book
- to learn the C syntax, or see the enclosed script examples to get a feeling
- of the language. I will not write a book about programming in C here, only
- tell you the essentials.
-
-
- PROGRAM
-
- A program is recognized as a script if it is in a TEXT file, and if the
- file name ends in ".s". Spaces, tabs and carriage return characters are
- considered to be white space. No identifier and no keyword can be separated
- by white space. A program consists of: comments, global variable
- definitions and function definitions. Everything included between "/*" and
- "*/" is considered a comment and will not be interpreted. Comments cannot
- be nested. The "/*" and "*/" are not recognized as comment delimiters
- inside string or character constants. Global variables are those variables
- that are known to all functions, unless their names are reused as local
- variables. Global variables can be initialized using any expression that
- involves either constants or other globals that are already defined at that
- point. Function definitions can appear in any order and their must be at
- least one function called "main" with no parameters. It is this function
- that is called when the script is started. Every function is supposed to
- return an integer value as result. If there is no return statement in a
- function, 0 will be returned. Functions can be called recursively. There
- are many built-in functions that are already defined when the script is
- started.
-
- The value the "main" function returns is used as follows:
- 0 : Don't restore saved settings, continue application
- 1 : Restore saved settings, continue application
- 256 : Don't restore saved settings, quit application
- 257 : Restore saved settings, quit application
- Restore saved settings means that all changes the script made to settings
- are forgotten as soon as the script finishes. Quit application means, that
- as soon as the script has finished, "Terminal" quits. Usually this means
- returning to the Finder. But if "Terminal" was launched from HyperCard it
- will return to HyperCard.
-
- This is an example of a simple script that displays the message "The number
- is 123" in the terminal window:
-
- int Number = 123; /* This is a global variable */
-
- main () /* Every script must have a main() function */
- {
- /* display() is a built-in function */
- display("The number is %i\r", Number);
- }
-
-
- IDENTIFIERS
-
- An identifier is a sequence of letters and digits; the first character must
- be a letter. The underscore "_" counts as a letter. An identifier can be of
- any length up to 255 characters. All characters are significant and are
- case sensitive. There are three types of identifiers: keywords, function
- names, variable names. The keywords are: "break", "char", "else", "for",
- "if", "int", "return", "while".
-
-
- CONSTANTS
-
- An integer constant is a sequence of decimal digits, or 0x followed by up
- to 8 hexadecimal digits. An integer value is represented internally by 4
- bytes (32 bits). A minus sign before a constant is considered as an unary
- operator, not a part of the constant itself. A character constant is a
- sequence of 1 to 4 characters enclosed in single quotes. Character
- constants are converted to integer values, the byte values corresponding to
- the ASCII codes of the characters. A string constant is a sequence of
- characters enclosed in double quotes. String constants are automatically
- terminated by a NULL character. The value of a string constant is the
- address of the first character. Some examples of valid constants:
-
- 1234567
- 0xABCD1234
- 'a'
- 'TEXT'
- "An apple a day keeps troubles away"
-
- In character and string constants the backslash "\" is used as an escape
- sequence according to the following table:
-
- LF 0x0A \n
- TAB 0x09 \t
- FF 0x0C \f
- BELL 0x07 \a
- BS 0x08 \b
- CR 0x0D \r
- VT 0x0B \v
- NULL 0x00 \0
- any 0x.. \x.. (2 hex digits)
-
- If the character following a backslash is not one of those specified the
- backslash is ignored. This can be used to represent the backslash itself,
- to put a single quote into a character constant or to put a double quote
- into a string constant. Some examples;
-
- "Going to the next line...\r"
- '\0'
- "This is a single backslash: \\"
- "\"Hello world!\""
- '\''
- '\x03' /* That's a control-C */
-
-
- VARIABLES
-
- There are four types of variables: characters, integers, pointers and
- arrays. Integers represent 32 bit signed values and characters 8 bit signed
- values. Pointers hold the 32 bit address of integers, characters or other
- pointers. Arrays are more or less equivalent to pointers. The following
- examples show how variables are defined:
-
- char c; /* "c" is a character variable */
- int n; /* "n" is an integer variable */
- char *ptr; /* "ptr" is a pointer to characters */
- char **hdl; /* "hdl" is a pointer to character pointers */
- int *p; /* "p" is a pointer to integers */
- int **q; /* "q" is a pointer to integer pointers */
- char a[80]; /* "a" points to an array of 80 characters (80 bytes) */
- int b[10]; /* "b" points to an array of 10 integers (40 bytes) */
- char *c[5]; /* "c" points to an array of 5 character pointers (20 b) */
- int *d[5]; /* "d" points to an array of 5 integer pointers (20 b) */
-
- Variable declarations can be grouped together like this:
-
- char c, *ptr, **hdl, a[80], *c[5];
- int n, *p, **q, b[10], *d[5];
-
- Variables can be initialized using any legal expression, not only
- constants. Arrays can only be initialized at the global level, i.e. outside
- of function definitions. Initialized arrays must not include their size
- between the square brackets, the size is calculated based on the
- initializing values.
-
- char c = 'x';
- int n = -123;
- int m = n * 10; /* "m" is set to -1230 */
- char message[] = "This is a character string";
- char *messages[] = /* Array of strings */
- { "This is string #1", "Another string", "and so on..." };
- int a[] = { 1, 2, 3, 4, 5 }; /* Array of 5 integers */
-
- The are no logical variables. Everything that is not zero is considered to
- be true.
-
-
- EXPRESSIONS
-
- The following is a list of the supported operators that build up an
- expression. The list is by increasing precedence. Normally operators group
- left to right unless otherwise noted below. Note that the assignment is an
- operator, that an expression may contain several assignments, and that an
- assignment returns a value. Logical and numerical operators can be freely
- mixed, everything not zero is considered to be true. The precedence level
- can be changed by using parentheses. The boolean operators will return 1 as
- true.
-
- Assignment = (right to left)
-
- Logical or ||
-
- Logical and &&
-
- Equal, not equal == !=
-
- Relational operators < <= > >=
-
- Add, subtract + -
-
- Multiply, divide, modulo * / %
-
- Logical not, increment,
- decrement, unary minus,
- indirection, address of ! ++ -- - * & (right to left)
-
- Function call, array element () []
-
- The increment or decrement operators can be used before or after a
- variable. If the operator comes before the variable (preincrement,
- predecrement) the variable is incremented, or decremented, and then the
- variable's value is used in the expression evaluation. If the operator
- comes after the variable (postincrement, postdecrement) the current value
- of the variable is used in the expression evaluation, and then the variable
- is incremented, or decremented.
-
- Array subscripts start with 0. There is no runtime check of array
- boundaries. An array subscript can be any valid expression. Some examples:
-
- char a[80];
- c = a[0]; /* The first element */
- c = a[79]; /* The last element */
- c = a[i = (79 - Func(x)*3)]; /* Expression as subscript */
-
- For pointer arithmetic the following rules apply: if an integer value is
- added or subtracted from a pointer (a pointer is an address) then the
- integer value is first scaled depending on what the pointer is pointing to:
- 1 for characters, 4 for integers, 4 for pointers. If two pointers are
- subtracted the result is scaled in the same way and yields an integer value
- representing the number of objects separated by the two pointers. The two
- pointers must point to the same type of objects.
-
- A function identifier without a parameter list in an expression results in
- the address of the function being used. If there is a parameter list, the
- function is called and the integer value returned by the function is used.
- Functions can also be called indirectly, using any expression that yields a
- valid function address followed by a parameter list. Some examples:
-
- int pf; /* "int" can be used as function pointer */
- int i;
- pf = Func; /* "pf" will contain address of function "Func" */
- i = Func(); /* "i" gets function result */
- i = (pf)(); /* "i" gets function result */
-
-
- FUNCTIONS
-
- A function definition can only occur in the outer, global level. A function
- cannot be defined inside another function. All functions are supposed to
- return integers. A function definition consists of the function name,
- followed by a parameter definition list enclosed in parentheses, followed
- by a compound statement. The parameter definition list describes the
- function parameters. These parameters are considered as local variables
- inside the function. Example:
-
- /* The following function takes 3 parameters: a character, an integer
- and a character pointer. It always returns the value 123. */
-
- Function (char c, int i, char *p)
- {
- /* ... */
- return 123;
- }
-
- A function call consists of the function name followed by a parameter list.
- The parameter list is a comma separated list enclosed in parentheses of
- expressions. All parameters are passed by value. There is no verification
- if the number of parameters is correct or if the values passed to a
- function correspond to the types of the declared parameters. Example:
-
- i = Function ('x', 4567 + x, "Hello");
-
-
- STATEMENTS
-
- A compound statement starts with an open brace "{" and terminates with a
- closing brace "}". There is an optional variable definition part followed
- by a list of statements. Variables defined inside a compound statement are
- local to that compound statement (block).
-
- A statement can be:
- compound statement
- expression ;
- if ( expression ) statement
- if ( expression ) statement else statement
- while ( expression ) statement
- for ( opt expr1 ; opt expr2 ; opt expr3 ) statement
- break ;
- return ;
- return expression ;
- ;
-
- The two forms of the conditional statement are:
- if ( expression ) statement
- if ( expression ) statement else statement
- In both cases the expression is evaluated and if it is nonzero, the first
- substatement is executed. In the second case the second substatement is
- executed if the expression is 0. The "else" ambiguity is resolved by
- connected an "else" with the last encountered "else"-less "if".
-
- The "while" statement has the form:
- while ( expression ) statement
- The substatement is executed repeatedly so long as the value of the
- expression remains nonzero. The test takes place before each execution of
- the statement.
-
- The "for" statement has the form:
- for ( opt expr1 ; opt expr2 ; opt expr3 ) statement
- This statement is equivalent to:
- expression1 ;
- while ( expression2 ) {
- statement
- expression3;
- }
- Thus the first expression specifies initialization for the loop, the second
- specifies a test, made before each iteration, such that the loop is exited
- when the expression becomes 0; the third expression often specifies an
- incrementation which is performed after each iteration. Any or all of the
- expressions may be dropped. A missing expression2 makes the implied "while"
- clause equivalent to while(1); other missing expressions are simply dropped
- from the expansion above.
-
- The statement
- break ;
- causes termination of the smallest enclosing "while" or "for" statement;
- control passes to the statement following the terminating statement.
-
- A function returns to its caller by means of the "return" statement, which
- has one of the forms:
- return ;
- return expression ;
- In the first case the returned value is undefined. In the second case the
- value of the expression is returned to the caller of the function. If
- required, the expression is converted to an integer. Flowing off the end of
- a function is equivalent to a return with no return value.
-
- The null statement has the form
- ;
- A null statement is useful to supply a null body to a looping statement
- such as "while".
-
- Note: Unlike real C, logical expressions are completely evaluated, even if
- their TRUEthness (different from 0) ore FALSEness (equal to 0) can be
- determined before the whole expression is evaluated. In real C that's
- called short circuit boolean evaluation. For example in the statement
- if (dothis() && dothat())
- ;
- the functions dothis() and dothat() are always called in script C. In real
- C the function dothat() is only called if dothis() returns a non-zero
- result.
-
-
- ___________________________________________________________________________
- INTRINSIC FUNCTIONS
-
-
- Intrinsic functions are built-in and need not to be defined in the script.
- They can be called by any script.
-
- All file access functions operate on the file transfer up- and download
- folder, which is the folder that can be set with the "Binary file transfer
- options..." menu item. File names can be up to 31 characters long and
- cannot contain the character ':'.
-
- All formatting functions ("display", "format", "type") use a template
- string to specify the formatting to be done on the following parameters.
- Format specifiers begin with the character % and include zero or more of
- the following conversion specification elements (optional fields are in
- brackets):
-
- % [option flags] [field size] conversion
-
- Some of these elements are optional, but if present, they must be specified
- in the order in which they are described below.
- Option flags (optional):
- - Left adjust output in field, pad on right (default is to right
- justify).
- 0 Use zero (0) rather than space for the pad character
- Field size specification (optional):
- The minimum field width, expressed as a decimal integer. The
- corresponding parameter will be printed in a field at least this wide.
- Conversion characters (required)
- c Parameter is a character
- i Parameter is an integer
- s Parameter is a null terminated string
- % Print a %, no parameter used
- The format specification elements must correspond to the following
- parameters. Some examples:
- char k, m[80];
- int n;
- type("Unrecognized character %c on line %i of file %s\r", k, n, m);
-
- The following list describes all the existing intrinsic functions. They are
- described in the so-called prototype format, i.e. the format you would have
- to use to define them. E.g.
- xxxx (char *s, int i)
- means that "xxxx" is a function taking 2 arguments, the first is a pointer
- to a character and the second is an integer. Note that all functions return
- integers, but the value returned is not necessarily meaningful for very
- function.
-
-
- autolf Set the auto linefeed flag on or off
-
- autolf (int flg)
- flg: 0 means off, 1 means on
-
-
- beep Macintosh system beep
-
- beep ()
-
-
- capture Start/stop capture to text file
-
- capture (int opt, char *name)
- result: Macintosh file system error, 0 if no error
- opt: 0 = Close capture file
- 1 = Capture on, new file
- 2 = Capture on, append to existing file
- name: Pointer to character string of at least 32 characters. This
- is the file name.
-
-
- catalog Return file info
-
- catalog (int i, char *name, int *type, int *dsize, int *rsize,
- int *cdate, int *mdate)
- result: Macintosh file system error, 0 if no error
- i: If 0, then name must be specified and information about the
- file with that name is returned, if it exists.
- If non-zero information (including file name) about the i-th
- file in the folder is returned.
- name: Pointer to character string of at least 32 characters. This
- is the file name (specified or returned depending on the value
- of i).
- type: Integer (4 bytes). File type.
- dsize: Data fork size in bytes.
- rsize: Resource fork size in bytes.
- cdate: Creation date in seconds.
- mdate: Modification date in seconds.
-
-
- date Convert seconds from Macintosh clock to date and time
-
- date (int sec, int *yr, int *mo, int *da, int *ho, int *mi,
- int *se, int *dw)
- sec: Seconds
- yr: Year (1904..20xx)
- mo: Month (1..12)
- da: Day (1..31)
- ho: Hour (0..23)
- mi: Minutes (0..59)
- se: Seconds (0..59)
- dw: Day of week (1..7, Sunday is 1)
-
-
- display Display in the terminal window
-
- display (char *templ, ...)
- templ: Template string
- ...: Variable number of arguments (maximum 9)
-
-
- download Download (receive) a binary file using X/Y/Z-Modem protocol
-
- download (char *name, int bin, int zmodem)
- result: Error code. 0 if Ok. 1 if timeout. 2 if cancel. 3 if abort (5
- consecutive control-X characters received). All other values
- are Macintosh file system errors.
- name: File name for new file (not used for Y-Modem batch or Z-Modem)
- bin: 1 to recognize and use MacBinary format
- zmodem: 0 use X/Y-Modem, 1 use Z-Modem
-
-
- format Formatted conversion of values into a string
-
- format (char *str, char *templ, ...)
- str: String for result
- templ: Template string
- ...: Variable number of arguments (maximum 8)
-
-
- free Dispose of the memory allocated by the new() function
-
- free (char *p)
- p: Pointer returned by previous call to new() function
-
-
- getcts Get current state of CTS input line
-
- getcts ()
- result: 0 if negated, 1 if asserted
-
-
- getdcd Get current state of DCD input line
-
- getdcd ()
- result: 0 if negated, 1 if asserted
-
-
- lecho Set the local echo flag on or off
-
- lecho (int flg)
- flg: 0 means off, 1 means on
-
-
- macrol Load new macros file
-
- macrol (char *name)
- result: Error code, 0 means no error
- name: File name of TEXT file with macros (folder will be the same as
- the "Terminal folder")
-
-
- macrox Execute macro
-
- macrox (int i, int op, char *name)
- result: Error code, 0 ok, 2 cancel, 3 abort (2 consecutive control-X
- characters received)
- i: Macro number (0 to 9)
- op: 0 send macro text (using send TEXT file parameters), 1 display
- macro text, 2 return macro name
- name: Only used if op is 2, should point to a string where macro
- name will be returned (at least 30 characters long)
-
-
- move Move bytes from source to destination
-
- move (char *src, char *dest, int cnt)
- src: Source pointer
- dest: destination pointer
- cnt: number of bytes to move
-
-
- new Allocate memory
-
- new (int size)
- result: Pointer to memory, or 0 if not enough memory available
- size: Number of bytes to allocate
-
-
- nextbuff Wait for some characters
-
- nextbuff (char *buff, int count, int t)
- result: count characters received, 1 timeout, 2 cancel, 3 abort
- (5 consecutive control-X characters received)
- count: Number of characters to wait for
- buff: Character buffer to hold received characters. Must be at least
- count characters long
- t: Timeout value in ticks (1/60th of second)
-
-
- nextline Wait for the next line received over the serial input port
-
- nextline (char *line, int t)
- result: 0 line received, 1 timeout, 2 cancel, 3 abort (5 consecutive
- control-X characters received)
- line: String to hold line, this will be a '\0' terminated string
- without the final carriage return
- t: Timeout value in ticks (1/60th of second)
-
-
- pause Pause for specified amount of time
-
- pause (int t)
- result: 1 timeout, 2 cancel.
- t: Timeout value in ticks (1/60th of second)
-
-
- prompt Wait for a prompt string to come over the serial input port
-
- prompt (char *str, int t)
- err: 0 string received, 1 timeout, 2 cancel, 3 abort (5 consecutive
- control-X characters received).
- str: String to wait for (case sensitive)
- t: Timeout value in ticks (1/60th of second)
-
-
- protocol Set binary file transfer options
-
- protocol (bin, qb, zmodem, autorx)
- bin: 0 no MacBinary, 1 use MacBinary, -1 no change
- qb: 0 no QuickB, 1 use QuickB, -1 no change
- zmodem: 0 Z/Y-Modem, 1 Z-Modem, -1 no change
- autorx: 0 no auto-receive, 1 Z-Modem auto-receive, -1 no change
-
-
- recho Set the remote echo flag on or off
-
- recho (int flg)
- flg: 0 means off, 1 means on
-
-
- save Set the save & display flag on or off
-
- save (int flg)
- flg: 0 means off, 1 means on
-
-
- send Send a text file over the serial output port
-
- send (char *name)
- result: error code. 0 if Ok. 2 if cancel. 3 abort (5 consecutive
- control-X characters received). All other values are Macintosh
- file system errors.
- name: File name
-
-
- setdtr Set DTR output line
-
- setdtr (int onoff)
- onoff: 0 to negate, 1 to assert
-
-
- setup Serial communications port setup
-
- setup (int baud, int data, int parity, int stop, char *port,
- int dtr, int hs)
- result: 0 if no error
- baud: 0=300, 1=600, 2=1200, 3=2400, 4=4800, 5=9600, 6=19200, 7=38400,
- 8=57600 baud (or -1 for no change)
- data: 0=7, 1=8 data bits (or -1 for no change)
- parity: 0=no, 1=even, 2=odd parity (or -1 for no change)
- stop: 0=1, 1=2 stop bit (or -1 for no change)
- port: port's name, e.g. "Modem Port" or "Printer Port"
- (or -1 for no change)
- dtr: 1=don't drop DTR when quitting (or -1 for no change)
- hs: Handshake 0=none, 1=XON/XOFF, 2=CTS, 3=DTR, 4=CTS/DTR
-
-
- stack Return free memory available for script heap and stack
-
- stack ()
- result: Combined free heap and stack space (bytes)
-
-
- strcmp Compare two strings (not case sensitive)
-
- strcmp (char *str1, char *str2)
- result: 0 if strings are equal, positive if str1 > str2, negative if
- str1 < str2
- str1: First string
- str2: Second string
-
-
- terminal Set terminal parameters
-
- terminal (int lecho, int recho, int autolf, int save)
- lecho: Local echo flag (or -1 if no modification)
- recho: Remote echo flag (or -1 if no modification)
- autolf: Auto line feed flag (or -1 if no modification)
- save: Save & capture flag (or -1 if no modification)
-
-
- text Set text file send parameters
-
- text (char *prompt, int lined, int chard)
- prompt: Prompt text
- lined: Ticks to wait after each line
- chard: Ticks to wait after each character
-
-
- time Return seconds from Macintosh clock
-
- time ()
- result: Seconds
-
-
- type Send a string over the serial output port
-
- type (char *templ, ...)
- templ: Template string
- ...: Variable number of arguments (maximum 9)
-
-
- upload Upload (transmit) a binary file using X/Y/Z-Modem protocol
-
- upload (char *name, int bin, int zmodem)
- result: error code. 0 if Ok. 1 if timeout. 2 if cancel. 3 abort (5
- consecutive control-X characters received). All other values
- are Macintosh file system errors.
- name: File name
- bin: 1 to recognize and use MacBinary format
- zmodem: 0 use X/Y-Modem, 1 use Z-Modem
-
-
- val Convert string to number
-
- val (char *str)
- result: Converted integer value
- str: Character string (0 terminated)
-
-
- xyparms Set binary file transfer options for X/Y-Modem
-
- xyparms (crc, k, batch, t)
- crc: 0 use checksum, 1 use CRC, -1 no change
- k: 0 use 128 Byte blocks, 1 use 1 KByte blocks "C",
- 2 use 1 KByte blocks "CK", -1 no change
- batch: 0 no batch (X-Modem), 1 batch official (Y-Modem),
- 2 batch RR (Y Modem), -1 no change
- t: timeout to use in ticks, -1 no change
-
-
- zparms Set binary file transfer options for Z-Modem
-
- zparms (ctl, t, retr, buf, pack, wind, crcq)
- ctl: 0 don't escape control characters, 1 escape control characters,
- -1 no change
- t: timeout to use in ticks, -1 no change
- retr: maximal retry count, -1 no change
- buf: receive buffer size in bytes, -1 no change
- pack: transmit sub-packet length in bytes, -1 no change
- wind: transmit window size in bytes, -1 no change
- crcq: transmit ZCRCQ spacing in bytes, -1 no change
-
-